/*
 * code.c
 *
 *  Created on: May 1, 2022
 *      Author: Abyss
 */

#include "main.h"

extern TIM_HandleTypeDef htim1;
extern TIM_HandleTypeDef htim2;
extern TIM_HandleTypeDef htim3;

extern UART_HandleTypeDef huart1;
extern UART_HandleTypeDef huart2;

uint8_t buffer,flag=0,data_buffer[32],Lock=0;

int16_t CH1,CH2,CH3,CH4,CH5,CH6,CH7,CH8;

uint16_t CH[18];  // 通道值


//懂得都懂
void run()
{
	HAL_TIM_Base_Start_IT(&htim1);
	HAL_UART_Receive_IT(&huart2, &buffer, 1);
	HAL_TIM_Base_Start_IT(&htim3);
	HAL_TIM_PWM_Start(&htim3, TIM_CHANNEL_1); // 打开 PWM
	HAL_TIM_PWM_Start(&htim3, TIM_CHANNEL_2); // 打开 PWM
	HAL_TIM_PWM_Start(&htim3, TIM_CHANNEL_3); // 打开 PWM
	HAL_TIM_PWM_Start(&htim3, TIM_CHANNEL_4); // 打开 PWM
	while(1)
	{
		if(flag==1)
		{
			//失控保护定时器
			 HAL_TIM_Base_Stop(&htim2);
			 __HAL_TIM_SET_COUNTER(&htim2,0);
			 HAL_TIM_Base_Start_IT(&htim2);
			 //HAL_UART_Transmit(&huart1,data_buffer,32,1000);
			 flag = 0;
			 CH1 = data_buffer[3];
			 CH1 = ((CH1<<8)|data_buffer[2])&0XFFF;

			 CH2 = data_buffer[5];
			 CH2 = ((CH2<<8)|data_buffer[4])&0XFFF;

		 	 CH3 = data_buffer[7];
		  	 CH3 = ((CH3<<8)|data_buffer[6])&0XFFF;

			 CH4 = data_buffer[9];
			 CH4 = ((CH4<<8)|data_buffer[8])&0XFFF;
			 printf("%d %d %d %d\r\n",CH1,CH2,CH3,CH4);


			 //油门回中  解锁电机
			 if(Lock == 1)
			 {
				 __HAL_TIM_SetCompare(&htim3,TIM_CHANNEL_1,CH3+1700);
			 }
			 else
			 {
				 if(CH3>1450&&CH3<1550)
				 {
					 Lock = 1;
				 }
			 }
			 __HAL_TIM_SetCompare(&htim3,TIM_CHANNEL_4,CH1+1000);
		}
	}
}


//定时器回调
void HAL_TIM_PeriodElapsedCallback(TIM_HandleTypeDef *htim)
{
  UNUSED(htim);
  //系统运行指示灯
  if(htim == &htim1)
  {
	  static int time = 0;
	  if(time++ == 500)
	  {
		 time=0;
		 HAL_GPIO_TogglePin(GPIOC, GPIO_PIN_13);
	  }
  }

  //失控保护
  if(htim == &htim2)
  {
	  Lock = 0;
	  HAL_GPIO_TogglePin(GPIOC, GPIO_PIN_13);
	  //停转
	  __HAL_TIM_SetCompare(&htim3,TIM_CHANNEL_1,3100);

	  HAL_TIM_Base_Stop(&htim2);
	 __HAL_TIM_SET_COUNTER(&htim2,0);
  }
}


//串口回调ibus协议
void HAL_UART_RxCpltCallback(UART_HandleTypeDef *huart)
{
 UNUSED(huart);
	 if(huart == &huart2)
	 {
		 flag = 1;
		 static int _buffer_flag0=0,_buffer_flag1=0,buffer_cnt=0;

		 if(buffer == 0x20&&buffer_cnt == 0&&_buffer_flag0 == 0&&_buffer_flag1 == 0)
		 {
			 _buffer_flag0 = 1;
			 data_buffer[0] = 0x20;
			 HAL_UART_Receive_IT(&huart2, &buffer, 1);
			 return ;
		 }

		 if(buffer == 0x40&&buffer_cnt == 0&&_buffer_flag0 == 1&& _buffer_flag1 == 0)
		 {
			 _buffer_flag1 = 1;
			 data_buffer[1] = 0x40;
			 HAL_UART_Receive_IT(&huart2, &buffer, 1);
			 return ;

		 }
		 if (_buffer_flag1 == 1)
		 {
			 data_buffer[2+buffer_cnt++] = buffer;
			 if(buffer_cnt == 30)
			 {
				flag = 1;
				_buffer_flag0 = 0;
				 _buffer_flag1=0;
				 buffer_cnt = 0;
			 }
		 }

		 HAL_UART_Receive_IT(&huart2, &buffer, 1);

	 }
}





//串口1重定向
#ifdef __GNUC__
#define PUTCHAR_PROTOTYPE int __io_putchar(int ch)
#else
#define PUTCHAR_PROTOTYPE int fputc(int ch, FILE *f)
#endif
PUTCHAR_PROTOTYPE
{
		HAL_UART_Transmit(&huart1 , (uint8_t *)&ch, 1, 0xFFFF);
		return ch;
}
